//
//  UPLoadViewCtl.m
//  afnleaks
//
//  Created by  Mac on 2021/3/31.
//

#import "UPLoadViewCtl.h"

@interface UPLoadViewCtl ()

@end

@implementation UPLoadViewCtl

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (IBAction)uploadaction:(id)sender {
    //1. 创建NSURLSessionConfiguration
       NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
       
       //2. 创建管理者对象
       AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
       
       //3. 设置url
       NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/1.mp4"];
       
       //4. 创建请求对象
       NSURLRequest *request = [NSURLRequest requestWithURL:url];
       
       //5. 下载任务
       NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
           //打印下载进度
           NSLog(@"%lf",1.0*downloadProgress.completedUnitCount/downloadProgress.totalUnitCount);
           
       } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
           //设置下载路径
           NSURL *documentsDirectoryURL = [[NSFileManager defaultManager]URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
           
           return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
           
       } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
           //下载完成
           NSLog(@"File downloaded to : %@",filePath);
       }];
       //启动任务
       [downloadTask resume];
}

@end
